package ug.co.icom.kyazze.healthticket.services;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import ug.co.icom.kyazze.healthticket.DataBase.Database;
import ug.co.icom.kyazze.healthticket.data.Allergy;
import android.app.IntentService;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class IntentUpdaterService extends IntentService {
	static final String TAG = "IntentUpdaterService";
	String doc_value;
	Database query_handler;
	Context context;
	String user_phone="";
	private String filepath = "healthTicket_files";

	public IntentUpdaterService() {
		super(TAG);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void onCreate() {
		Log.d(TAG, "onCreated");
		
		

		super.onCreate();
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		SharedPreferences settings = getSharedPreferences("ULS", 0);
		user_phone = settings.getString("uName", null);
		
		File directory;
		
		ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
		directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
		@SuppressWarnings("unused")
		File file3= new File(directory+"/"+settings.getString("uName", null)+".cpabe");
		
		
		try {

			BufferedReader br = null;

			try {

				String sCurrentLine;

				br = new BufferedReader(new FileReader(directory
						+ "/"
						+ user_phone + ".txt"));

				while ((sCurrentLine = br.readLine()) != null) {
					doc_value = sCurrentLine;
					System.out.println(sCurrentLine);
				}

			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (br != null)
						br.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}

			SQLiteDatabase db = openOrCreateDatabase("mrDB", MODE_PRIVATE, null);
			query_handler = new Database(db);

			ArrayList<Allergy> all_object = query_handler
					.getUnSyc_AllAllergies();

			String a_name = all_object.get(0).getAllergy_name();
			String a_date = all_object.get(0).getAllergy_date_of_diagnosis()
					.toString();
			String a_severity = all_object.get(0).getAllergy_severity();
			String a_reaction = all_object.get(0).getAllergy_reaction();
			String a_occurence = all_object.get(0).getAllergy_occurence();
			String a_comments = all_object.get(0)
					.getAllergy_additional_comments();

			// try catch block
			PutAllergyData doc = new PutAllergyData();
			// String val = "{\"$oid\":\"52f1e542eddcb50002001236\"}";
			doc.execute(doc_value, a_name, a_date, a_severity, a_reaction,
					a_occurence, a_comments);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy");
		super.onDestroy();
	}

}
